package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.constants.bank.GdbConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.gdb.ebank.GdbB2cMessage;

/**
 * 广发银行WAP网关支付结果
 * 
 * @GdbWapServerRes.java
 * @author lilili
 * @2017-04-01 上午11:38:40 www.gopay.com.cn Inc.All rights reserved.
 */
@Controller
@Scope("prototype")
public class GdbWapServerRes extends BankResBase {

	 private static Log logger = LogFactory.getLog(GdbWapServerRes.class);

	    @RequestMapping(value = "/gdbWapServerRes")
	    protected ModelAndView execute(HttpServletRequest request, HttpServletResponse response) {
	        logger.error("--------------- GdbWapServerRes bankback ---------------");
	        return super.execute(request, response);
	    }

	    @Override
	    protected BaseMessage buildMessage(HttpServletRequest request) {
	        GdbB2cMessage gdbMessage = new GdbB2cMessage();
	        gdbMessage.setBank(BankConstants.BANK_LOGO_GDB);
	        String amt = request.getParameter("orderAmt");
	        String orderNo = request.getParameter("orderNo");
	        String orderdate = request.getParameter("orderDate");
	        String ordertime = request.getParameter("transferTime");
	        String payState = request.getParameter("payState");
	        String signInfo = request.getParameter("orderSignInfo");
	        String merid = request.getParameter("commerceId");
	        String transferFlowNo = request.getParameter("transferFlowNo");

	        gdbMessage.setSignMessage(signInfo);
	        gdbMessage.setBankOrderId(orderNo);
	        gdbMessage.setAmount(new BigDecimal(amt));
	        gdbMessage.setPayState(payState);
	        gdbMessage.setOrderDate(orderdate);
	        gdbMessage.setOrderTime(ordertime);
	        gdbMessage.setMerchId(merid);
	        gdbMessage.setTransferFlowNo(transferFlowNo);
	        String plainMessage = buildPlainB2C(gdbMessage);
	        gdbMessage.setPlainMessage(plainMessage);
	        
	        // 银行业务产品11个人wap网银支付
	        gdbMessage.setBankBusProd(BisBankBusinessProd._11);
	        return gdbMessage;

	    }
	    
	    /**
		 * 构建B2C报文
		 * 
		 * @param message
		 *            广发银行B2C数据结构体
		 * @return String 商户验签源数据
		 */
		public String buildPlainB2C(GdbB2cMessage message) {
			StringBuffer sb = new StringBuffer("");
			sb.append(message.getBankOrderId()).append("|")
			.append(message.getAmount()).append("|")
			.append(message.getOrderTime()).append("|")
			.append(message.getTransferFlowNo()).append("|")
			.append(message.getMerchId());
			return sb.toString();
		}

	    @Override
	    protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
	        OrderInfo orderInfo = new OrderInfo();
	        GdbB2cMessage gdbMessage = (GdbB2cMessage) result.getModel("message");
	        if (gdbMessage != null) {
	            if (GdbConstants.WAP_PAY_STATUS_SUCCESS.equals(gdbMessage.getPayState())) {
	                orderInfo.setOrderId(gdbMessage.getOrderId());
	                orderInfo.setAmt(gdbMessage.getAmount().toString());
	                orderInfo.setNotifyNum("1");
	                orderInfo.setPayStatus("00");
	                orderInfo.setRetCode("00");
	                orderInfo.setRetMsg("");
	            } else { 
	                orderInfo.setOrderId(gdbMessage.getOrderId());
	                orderInfo.setAmt(gdbMessage.getAmount().toString());
	                orderInfo.setNotifyNum("1");
	                orderInfo.setPayStatus("01");
	                orderInfo.setRetCode("01");
	                orderInfo.setRetMsg("");
	            }
	        }
	        return orderInfo;
	    }

	    @Override
	    protected boolean checkParameters(HttpServletRequest request) {
	        if (StringUtils.isEmpty(request.getParameter("orderNo")) || StringUtils.isEmpty(request.getParameter("orderSignInfo"))) {
	            return false;
	        }
	        return true;
	    }

}
